package com.multiplayer.dao.impl;

import java.sql.Connection;
import java.util.List;

import com.multiplayer.dao.IUserDao;
import com.multiplayer.entity.Student;
import com.multiplayer.entity.User;
import com.multiplayer.tools.DBTools;
import com.multiplayer.tools.ITransaction;
import com.multiplayer.tools.PageData;

public class UserDaoImpl implements IUserDao{
	
	
	@Override
	public boolean saveUser(User user) {
		String sql = "insert into user(Userid, Sno, Upassword, Credibility)values(?,?,?,?)";
	
		int result = (Integer)DBTools.transaction(new ITransaction() {
			
			@Override
			public Object execute(Connection conn) {
				// TODO Auto-generated method stub
				int n1 = 0;
				try {
					n1 = DBTools.exUpdate(sql, conn, user.getUserid(),user.getSno(),user.getUpassword(),user.getCredibility());
				} catch (Exception e) {
					// TODO Auto-generated catch block
					throw new RuntimeException("保存失败，事务回滚!", e);
				}
				return n1;
			}
		});
		return result>0;
	}

	@Override
	public boolean deleteUserById(String userId) {
		String sql = "delete from user where userid = ?";
		int result = (Integer)DBTools.transaction(new ITransaction() {
			
			@Override
			public Object execute(Connection conn) {
				int n1 = 0;
				try {
					n1 = DBTools.exUpdate(sql, conn, userId);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					throw new RuntimeException("删除失败，事务回滚!", e);
				}
				return n1;
			}
		});
		return result>0;
	}

	@Override
	public boolean editUser(User user) {
		String sql = "update user set userid=?,upassword=?,credibility=?";
		int result = (Integer)DBTools.transaction(new ITransaction() {
			
			@Override
			public Object execute(Connection conn) {
				// TODO Auto-generated method stub
				int n1 = 0;
				try {
					n1 = DBTools.exUpdate(sql, conn, user.getUserid(),user.getUpassword(),user.getCredibility());
				} catch (Exception e) {
					// TODO Auto-generated catch block
					throw new RuntimeException("修改失败，事务回滚!", e);
				}
				return n1;
			}
		});
		return result>0;
	}

	@Override
	public List<User> queryUserByKeywords(String keywords) {
		String sql = "select userid, sno, upassword, credibility from user where userid like ? or sno like ?";
		List<User> list = (List<User>)DBTools.exQuery(sql, User.class, "%"+keywords+"%","%"+keywords+"%");
		return list;
	}

	@Override
	public List<User> queryUserById(String userId) {
		String sql = "select userid, sno, upassword, credibility from user where Userid = ?";
		List<User> list = (List<User>)DBTools.exQuery(sql, User.class,userId);
		return list;
	}

	@Override
	public PageData queryUserPageDate(Integer page, Integer pageSize) {
		//1.查询到User信息
		String sql = "select userid, sno, upassword, credibility from user";
		PageData pageData = DBTools.exQueryByPage(sql, User.class, page, pageSize);
		//2.查询User对应的学生信息
		List<User> list = pageData.getData();
		for(User u : list) {
			//根据用户的id查询对应的student
			List<Student> list1 = queryStudent(u.getSno());
			for(Student s:list1) {
				//用户的student属性设置成查询的结果
				u.setStudent(s);
			}
		}
		return pageData;
	}

	@Override
	public List<Student> queryStudent(String username) {
		String sql = "select sno, sname, ssex, sage, sclass, sacademy from student where sno = ?";
		List<Student> list = (List<Student>)DBTools.exQuery(sql, Student.class, username);
		return list;
	}

}
